package cz.zcu.fav.ratdroid.activity.calendar.day;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;

import cz.zcu.fav.ratdroid.bean.Period;

public class Column implements Serializable
{
	private static final long serialVersionUID = 8792298034488120846L;
	List<Period> periods;

	public Column()
	{
		periods = new ArrayList<Period>();
	}

	public boolean addNewPeriod(Period newPeriod)
	{
		LocalDateTime fromNew = newPeriod.getFromDate();
		LocalDateTime toNew = newPeriod.getToDate();

		if (isColumnEmpty(fromNew, toNew))
		{
			periods.add(newPeriod);
			return true;
		}
		return false;
	}

	public boolean isColumnEmpty(LocalDateTime fromNew, LocalDateTime toNew)
	{
		for (Period period : periods)
		{
			LocalDateTime fromOld = period.getFromDate();
			LocalDateTime toOld = period.getToDate();

			if (((fromNew.isAfter(fromOld) || fromNew.isEqual(fromOld))
					&& fromNew.isBefore(toOld) || fromNew.isEqual(toOld))
					|| ((fromNew.isBefore(fromOld) || fromNew.isEqual(fromOld)) && (toNew
							.isAfter(fromOld) || toNew.isEqual(fromOld))))
			{
				return false;
			}
		}
		return true;
	}

	public List<Period> getPeriods()
	{
		return periods;
	}

	// LocalDateTime from, LocalDateTime from2, LocalDateTime from3,
	// LocalDateTime to);
	//
	// AND ((? &gt;= period.fromDate AND ? &lt;= period.toDate) OR
	// (? &lt;= period.fromDate AND ? &gt;= period.fromDate) )

}
